package com.hospital.ecard.cardhandler;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.common.core.module.entity.ecard.BarcodeLogEntity;
import com.hospital.common.core.module.entity.ecard.ResponseEntity;
import com.hospital.common.core.utils.SpringContextHolder;
import com.hospital.ecard.module.entity.PatientInfo;
import com.hospital.ecard.module.service.PatientInfoService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lvyao
 * @description 患者ID号获取基本信息
 * @date 2020/8/24
 */
public class InpNoHandler implements CardHandler {

    private PatientInfoService patientInfoService = SpringContextHolder.getBean(PatientInfoService.class);

    @Override
    public String dealCardInfo(BarcodeLogEntity barcodeLogEntity, HttpServletRequest request) {
        ResponseEntity<PatientInfo> cardInfoResEntityResponseEntity = new ResponseEntity<>();
        List<PatientInfo> patientInfoList = null;
        QueryWrapper<PatientInfo> patientInfoEntityWrapper = new QueryWrapper<>();
//        if (barcodeLogEntity.getIdentityNo().split("=").length > 0) {
        String s = barcodeLogEntity.getIdentityNo();
        patientInfoEntityWrapper.eq("inp_no", s);
        if (barcodeLogEntity.isCardFlag()) {
            patientInfoEntityWrapper.eq("card_status", "5");
        }
        cardInfoResEntityResponseEntity = new ResponseEntity<>();
        patientInfoList = patientInfoService.list(patientInfoEntityWrapper);
//        }
        if (patientInfoList != null && patientInfoList.size() > 0) {
            cardInfoResEntityResponseEntity.setIsSuccess(10000);
            cardInfoResEntityResponseEntity.setBusinessInfo(patientInfoList.get(0));
        } else {
            cardInfoResEntityResponseEntity.setIsSuccess(10010);
            cardInfoResEntityResponseEntity.setErrMsg("未获取到病人信息");
        }
        return JSONObject.toJSONString(cardInfoResEntityResponseEntity, SerializerFeature.WriteMapNullValue);
    }
}
